<template>
  <div class="view_log">
    <log-detail ref="logDetail" maxHeight="240">
      <div class="handleDetail" v-if="statusProcessed">
        <div class="handle">处理意见</div>
        <ul class="workStage">
          <li>
            <h5>处理原因</h5>
            <p>
              <span>{{reasonData}}</span>
            </p>
          </li>
          <li class="border_top">
            <h5>处理意见</h5>
            <p>
              <span>{{list.remarks}}</span>
            </p>
          </li>
          <li class="border_top"
              v-if="list.reworkQuantity ">
            <h5>返回工序</h5>
            <p>
              <span>{{list.reworkQuantity && list.reworkQuantity > 0 ? '返工返修-' + list.reworkWorkstageName : ''}}</span>
            </p>
          </li>
          <li class="border_top"
              v-if="list.reworkQuantity || list.scrapQuantity ||list.degradeQuantity || list.isNewBatchId">
            <h5>任务类型</h5>
            <p>
              <span>{{list.reworkQuantity && list.reworkQuantity > 0 ? '返工返修任务'+ ',' : ''}}</span>
              <span>{{list.scrapQuantity && list.scrapQuantity > 0 ? '报废任务'+ ',' : ''}}</span>
              <span>{{list.degradeQuantity && list.degradeQuantity > 0 ? '降级使用任务'+ ',' : ''}}</span>
              <span>{{list.surplusQuantity && list.surplusQuantity > 0 ? '余量自动拆分任务': ''}}</span>
            </p>
          </li>
          <li class="border_top"
              v-if="list.reworkQuantity || list.scrapQuantity ||list.degradeQuantity || list.surplusQuantity">
            <h5>明细</h5>
            <p>
              <span>
                {{list.reworkQuantity && list.reworkQuantity > 0 ? '返工返修数量(' + list.reworkQuantity + ')'+
                (list.reworkNumber ?  '：'+ list.reworkNumber :'')+',' : ''}}
              </span>
              <span>
                {{list.scrapQuantity && list.scrapQuantity > 0 ? '当前报废数量(' + list.scrapQuantity + ')'+
                (list.scrapNumber ?  '：'+ list.scrapNumber :'')+',' : ''}}
              </span>
              <span>
                {{historyScrapQuantity > 0 ? '报废总数量(' + historyScrapQuantity + '),': ''}}
              </span>
              <span>
                {{list.degradeQuantity && list.degradeQuantity > 0 ? '降级使用数量(' + list.degradeQuantity + ')' +
                (list.degradeNumber ?  '：'+ list.degradeNumber :'')+',' : ''}}
              </span>
              <span>
                {{list.surplusQuantity && list.surplusQuantity > 0 ? '余量自动拆分数量(' + list.surplusQuantity + ')：'+list.surplusTurnoverNumber: ''}}
              </span>
            </p>
          </li>
        </ul>
      </div>
    </log-detail>
  </div>
</template>

<script>
export default {
  name: 'ViewLog',
  data() {
    return {
      isLoading: false,
      list: {}, // 处理结论详细信息
      reasonData: {}, // 处理原因
      statusProcessed: true, // 显示处理处理结论
      historyScrapQuantity: 0
    }
  },
  methods: {
    openDialog(row) {
      this.$refs.logDetail.openDialog({
        title: '周转码',
        name: row.turnoverNumber,
        url: 'a/turnoverstatuschangehistory/turnoverStatusChangeHistory/list',
        query: {
          turnoverBatchId: row.turnoverId
        }
      })
      this.isLoading = true
      // 处理状态为已处理时显示处理结论
      this.statusProcessed = row.problemProcessStatusValue === 'Processed'
      this.hanldeConclude(row)
      this.hanldeReason(row)
      this.getHistoryScrapQuantity(row)
    },
    // 获取历史报废总数
    getHistoryScrapQuantity(row) {
      let postData = {
        method: 'get',
        url: 'a/workstageproblemresult/workstageProblemResult/countScrapQuantity/' + row.turnoverWstageId,
        noCommit: true
      }
      this.$store.dispatch('saveData', postData).then((res) => {
        this.historyScrapQuantity = res.data || 0
        console.log(res.data)
      })
    },
    // 处理意见
    hanldeConclude(row) {
      let postData = {
        method: 'post',
        url: 'a/workstageproblemresult/workstageProblemResult/list',
        module: 'problemProcess',
        data: {
          pageNo: 1,
          pageSize: 9999,
          query: {
            problemId: row.id
          }
        },
        state: 'concludeList'
      }
      this.$store.dispatch('saveData', postData).then((res) => {
        this.list = res.list[0] || {}
      })
    },
    // 处理原因
    hanldeReason(row) {
      let postData = {
        method: 'post',
        url: 'a/workstageproblemreason/workstageProblemReason/list',
        module: 'problemProcess',
        data: {
          pageNo: 1,
          pageSize: 9999,
          query: {
            problemId: row.id
          }
        },
        state: 'reasonList'
      }
      this.$store.dispatch('saveData', postData).then((res) => {
        let str = ''
        res.list.map(row => {
          str += row.reasonLabel + ','
          this.reasonData = str
        })
      })
    },
  }
}
</script>

<style lang="less">
  .view_log {
    .handle {
      padding: 10px 0;
    }

    .workStage {
      list-style: none;
      padding: 0;
      margin: 0;

      li {
        height: 38px;
        line-height: 38px;
        border: 1px solid #dbe1e8;

        h5 {
          margin: 0;
          width: 170px;
          font-size: 14px;
          text-align: center;
          display: inline-block;
          font-weight: normal;
          border-right: 1px solid #dbe1e8;
          background-color: #ebf0f6;
        }

        p {
          display: inline-block;
        }
      }

      .border_top {
        border-top: none;
      }
    }
  }

</style>
